package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Hospital;
import com.itheima.service.HospitalService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/hospital")
public class HospitalController {

    @Reference
    private HospitalService hospitalService;

    @Autowired
    private JedisPool jedisPool;

    //图片上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile")MultipartFile imgFile){
        try{
            //获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");
            //获取文件后缀
            String suffix = originalFilename.substring(lastIndexOf - 1);
            //使用UUID随机产生文件名称，防止同名文件覆盖
            String fileName = UUID.randomUUID().toString() + suffix;

            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);

            //图片上传成功
            Result result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS);
            result.setData(fileName);

            //将上传图片名称存入Redis，基于Redis的Set集合存储
            jedisPool.getResource().sadd(RedisConstant.HOSPITAL_PIC_RESOURCES, fileName);

            return result;
        }catch (Exception e){
            e.printStackTrace();
            //图片上传失败
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    //新增医院
    @RequestMapping("/add")
    public Result add(@RequestBody Hospital hospital){
        try {
            hospitalService.add(hospital);
        }catch (Exception e){
            //新增套餐失败
            return new Result(false, MessageConstant.ADD_HOSPITAL_FAIL);
        }
        //新增套餐成功
        return new Result(true, MessageConstant.ADD_HOSPITAL_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = hospitalService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }
    //按照id查询医院
    @RequestMapping("/findById")
    public Result findBysId(Integer id){
        try {
            Hospital hospital = hospitalService.findById(id);
            return new Result(true,MessageConstant.QUERY_HOSPITAL_SUCCESS,hospital);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_HOSPITAL_FAIL);
        }
    }


    //查找全部
    @RequestMapping("findAll")
    public Result findAll(){

        List<Hospital> hospitalList =hospitalService.findAll();
        if (hospitalList!=null&&hospitalList.size()>0){
            //查询成功
            return new Result(true,MessageConstant.QUERY_HOSPITAL_SUCCESS,hospitalList);
        }else{
            //查询失败
            return new Result(false,MessageConstant.QUERY_HOSPITAL_FAIL);
        }
    }

    //编辑医院信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody Hospital hospital){
        try {
            hospitalService.edit(hospital);
            return new Result(true,MessageConstant.EDIT_HOSPITAL_SUCCESS);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_HOSPITAL_FAIL);
        }
    }

    //删除医院信息
    @RequestMapping("/delete")
    public Result delete(Integer id){
        Result result=null;
        try {
           result = hospitalService.delete(id);
        }catch (Exception e){
            return result;
        }
        if (result.isFlag()){
            //flag为true,删除成功
            return result;
        }else {
            //删除失败存在关系
            List<Integer> list = (List<Integer>) result.getData();
            StringBuilder s = new StringBuilder();
            for (int i = 0; i < list.size(); i++) {
                if (i==list.size()-1){
                    s.append(list.get(i)).append("号.");
                }else {
                    s.append(list.get(i)).append("号,");
                }
            }
            result.setMessage(MessageConstant.DELETE_HOSPITAL_FAIL+",存在以下订单:"+s.toString());
            return result;
        }
    }

}
